package com.cn.Dao;

import com.cn.base.BaseDao;
import com.cn.entity.NewsInfo;
import com.cn.entity.UserInfo;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class NewsinfoDao {
    public List<NewsInfo> findAll() throws SQLException {
        Connection connection = BaseDao.getConn();
        try {
            Statement state=connection.createStatement();//容器
            String sql="select * from newspaper_info";           //sql语句
            ResultSet rs = state.executeQuery(sql);     //将sql语句传至数据库，返回的值为一个字符集用一个变量接收

            List<NewsInfo> newsInfos = getList(rs);
            connection.close();
            return newsInfos;
        }catch (Exception e){
            throw e;
        }
    }
    public NewsInfo findByName(String name) throws SQLException {
        Connection connection = BaseDao.getConn();
        try {
            Statement state=connection.createStatement();
            String sql="select * from newspaper_info where NEWSINFO_NUM='"+name+"'";
            ResultSet rs = state.executeQuery(sql);

            NewsInfo newsInfo = getNews(rs);
            connection.close();
            return newsInfo;
        }catch (Exception e){
            throw e;
        }
    }
    private List<NewsInfo> getList(ResultSet resultSet){
        try {
            List<NewsInfo> newsInfos=new ArrayList<>();
            while (resultSet.next()){
                NewsInfo newsInfo = new NewsInfo();
                newsInfo.setNum(resultSet.getString(1));
                newsInfo.setName(resultSet.getString(2));
                newsInfo.setId(resultSet.getInt(3));
                newsInfo.setPub(resultSet.getString(4));
                newsInfo.setTerm(resultSet.getString(5));
                newsInfo.setCon(resultSet.getString(6));
                newsInfo.setPic(resultSet.getBytes(7));
                newsInfo.setPrice(resultSet.getFloat(8));
                newsInfos.add(newsInfo);
            }
            return newsInfos;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    private NewsInfo getNews(ResultSet resultSet){
        try {
            NewsInfo newsInfo = new NewsInfo();
            while (resultSet.next()){
                newsInfo.setNum(resultSet.getString(1));
                newsInfo.setName(resultSet.getString(2));
                newsInfo.setId(resultSet.getInt(3));
                newsInfo.setPub(resultSet.getString(4));
                newsInfo.setTerm(resultSet.getString(5));
                newsInfo.setCon(resultSet.getString(6));
                newsInfo.setPic(resultSet.getBytes(7));
                newsInfo.setPrice(resultSet.getFloat(8));
            }
            return newsInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
     public void Insert(NewsInfo newsInfo) throws SQLException {
        Connection connection = BaseDao.getConn();
        try{
            PreparedStatement preparedStatement=null;
            String sql2 = "INSERT INTO newspaper_info(NEWSINFO_NUM,NEWSINFO_NAME,NEWS_ID,NEWSINFO_PUB,NEWSINFO_TERM,NEWSINFO_CON,NEWSINFO_PIC,NEWSINFO_PRICE) VALUES(?,?,?,?,?,?,?,?)";
            System.out.println(sql2);
            InputStream inputStream=new FileInputStream(newsInfo.getPath());
            preparedStatement = connection.prepareStatement(sql2);
            preparedStatement.setString(1,newsInfo.getNum());
            preparedStatement.setBlob(7,inputStream);
            preparedStatement.setString(2,newsInfo.getName());
            preparedStatement.setInt(3,newsInfo.getId());
            preparedStatement.setString(4,newsInfo.getPub());
            preparedStatement.setString(5,newsInfo.getTerm());
            preparedStatement.setString(6,newsInfo.getCon());
            preparedStatement.setFloat(8,newsInfo.getPrice());
            //sql="update newspaper_info set NEWSINFO_PIC = '"+newsInfo.getPic()+"' where NEWSINFO_NUM = '"+newsInfo.getNum()+"'";
            preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            connection.close();
        }
    }

    public NewsInfo findByNum(String num) throws SQLException {
        Connection connection = BaseDao.getConn();
        try {
            Statement state=connection.createStatement();
            String sql="select * from newspaper_info where NEWSINFO_NUM='"+num+"'";
            ResultSet rs = state.executeQuery(sql);
            NewsInfo newsInfo = getNews(rs);
            connection.close();
            return newsInfo;
        }catch (Exception e){
            throw e;
        }
    }
}
